package com.ssyt.ec.assit.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.ec.assit.entity.AssistEntity;
import com.ssyt.ec.assit.entity.AssistMinuteEntity;
import com.ssyt.ec.assit.entity.AssistUpdateSearchEntity;
import com.ssyt.ec.assit.entity.MenuSearchEntity;
import com.ssyt.ec.assit.service.AssistService;
import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.common.util.SysuserUtil;

@WebServlet("/AssistServlet")
public class AssistServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
	private AssistService assistService = new AssistService();

	public AssistServlet() {
		super();

	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if ("listAssistStationQuery".equals(command)) {
			listAssistStationQuery(request, response);                       
		} else if ("updateState".equals(command)) {
			updateState(request, response);
		} else if ("listAssistStationMinute".equals(command)) {
			listAssistStationMinute(request, response);
		} else if ("senderCombobox".equals(command)) {
			senderCombobox(request, response);
		} else if ("searchNameAdd".equals(command)) {
			searchNameAdd(request, response);
		} else if ("appointClassnameAdd".equals(command)) {
			appointClassnameAdd(request, response);
		} else if ("assistInfoAdd".equals(command)) {
			assistInfoAdd(request, response);
		} else if ("assistInfoUpdate".equals(command)) {
           assistInfoUpdate(request, response);
           
		}else if("listUpdateMinute".equals(command))
		{
			 listUpdateMinute(request, response);
		}
	}

	/**
	 * 辅助模块-站内信息查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listAssistStationQuery(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		// 条件查询获取值
		// 标题
		String titleSearch = request.getParameter("titleSearch");
		// //发送角色
		String senderSearch = request.getParameter("senderSearch");
		// //是否过期
		int expireSearchInt = 0;
		String expireSearch = request.getParameter("expireSearch");
		if (StringUtils.isNotBlank(expireSearch)) {
			expireSearchInt = Integer.parseInt(expireSearch);
		}
		// 状态
		int infoStateSearchInt = 0;
		String infoStateSearch = request.getParameter("infoStateSearch");
		if (StringUtils.isNotBlank(infoStateSearch)) {
			infoStateSearchInt = Integer.parseInt(infoStateSearch);
		}
		// 类型
		String sendTypeSearch = request.getParameter("sendTypeSearch");
		if (sendTypeSearch == null) {
			sendTypeSearch = "1";
		}
		// //登录用户ID
		int userId = SysuserUtil.getUserId(request);
		PageModel<AssistEntity> assistEntity = assistService
				.listAssistStationQuery(userId, page, rows, titleSearch,
						sendTypeSearch, senderSearch, infoStateSearchInt,
						expireSearchInt);
		print(response, FastjsonUtil.toJson(assistEntity));
	}

	/**
	 * 操作业务
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updateState(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int rowId = Integer.parseInt(request.getParameter("id"));
		int stateIndex = Integer.parseInt(request.getParameter("value"));
		if (stateIndex == 1) {
			stateIndex = 3;
		} else if (stateIndex == 2) {
			stateIndex = 1;
		} else {
			stateIndex = 1;
		}
		int row = assistService.updateState(rowId, stateIndex);
		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 站内详细查看
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listAssistStationMinute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		AssistMinuteEntity assistEntity = assistService
				.listAssistStationMinute(id);
		print(response, FastjsonUtil.toJson(assistEntity));
	}

	/**
	 * 条件查询发送角色下拉生成
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void senderCombobox(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<MenuSearchEntity> senderCombobox = assistService.senderCombobox();
		print(response, FastjsonUtil.toJson(senderCombobox));
	}

	/**
	 * 指定用户名的下拉查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void searchNameAdd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<MenuSearchEntity> userName = assistService.searchNameAdd();
		print(response, FastjsonUtil.toJson(userName));
	}

	/**
	 * 指定班级的下拉查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void appointClassnameAdd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<MenuSearchEntity> appointClassnameAdd = assistService
				.appointClassnameAdd();
		print(response, FastjsonUtil.toJson(appointClassnameAdd));
	}

	/**
	 * 站内新增
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void assistInfoAdd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String titleName = request.getParameter("titleAdd");
		String content = request.getParameter("content");
		int receiver_user_ids = Integer.parseInt(request
				.getParameter("acceptname"));
		int receiver_class_ids = Integer.parseInt(request
				.getParameter("classname"));
		String expire_time = request.getParameter("expire_time");
		int state = Integer.parseInt(request.getParameter("infoStateSearch"));
		int userId = SysuserUtil.getUserId(request);
		int row = assistService.assistInfoAdd(titleName, content,
				receiver_user_ids, receiver_class_ids, expire_time, state,
				userId);
		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 站内信息修改
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void assistInfoUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id =Integer.parseInt( request.getParameter("id"));
		 String title = request.getParameter("title");
		 String content = request.getParameter("content");
		 String acceptname =request.getParameter("acceptname");
		 String classname = request.getParameter("classname");
		 String expire_time = request.getParameter("expire_time");
		 int state = Integer.parseInt( request.getParameter("state"));
		 int row = assistService.assistInfoUpdate(id,title,content,acceptname,classname,expire_time,state);
		 if(row !=0){
			 print(response, 1);
		 }
		 
	}
	
	/**
	 * 修改查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listUpdateMinute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			int rowId = Integer.parseInt(request.getParameter("id"));
			AssistUpdateSearchEntity assistUpdateSearchEntity = assistService.listUpdateMinute(rowId);
			print(response, FastjsonUtil.toJson(assistUpdateSearchEntity));
	}
}
